package Tester;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Random;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.junit.Test;

import DataStorageClasses.Client;
import DataStorageClasses.Employee;
import DataStorageClasses.Job;
import Database.DatabaseConnector;

public class TestClient {

	private static HttpClient httpclient = new DefaultHttpClient();
	DatabaseConnector db = new DatabaseConnector();
	static String randEmp = "";
	static String randClient = "";
	public static String getAsString(String method, String url) throws ClientProtocolException, IOException {
		if(method.equals("get"))
		{
			HttpGet httpget = new HttpGet(url);
			HttpResponse response = httpclient.execute(httpget);
			HttpEntity entity = response.getEntity();
			char[] buf = new char[1024];
			StringBuffer str = new StringBuffer();
			if (entity != null) {
			    BufferedReader br = new BufferedReader(new InputStreamReader(entity.getContent()));
			    int read = br.read(buf);
			    while(read != -1) {
			    	str.append(buf, 0, read);
			    	read = br.read(buf);
			    }
			}
			return str.toString();
		}
		else if(method.equals("post"))
		{
			HttpPost httpPost = new HttpPost(url);
			HttpResponse response = httpclient.execute(httpPost);
			HttpEntity entity = response.getEntity();
			char[] buf = new char[1024];
			StringBuffer str = new StringBuffer();
			if (entity != null) {
			    BufferedReader br = new BufferedReader(new InputStreamReader(entity.getContent()));
			    int read = br.read(buf);
			    while(read != -1) {
			    	str.append(buf, 0, read);
			    	read = br.read(buf);
			    }
			}
			return str.toString();
		}
		
		return "fail";
		
	}
	
	@Test
	public static String randString(Random rng, String characters, int length)
	{
	    char[] text = new char[length];
	    for (int i = 0; i < length; i++)
	    {
	        text[i] = characters.charAt(rng.nextInt(characters.length()));
	    }
	    return new String(text);
	}
	
	public void getRandEmpl()
	{
		Random num = new Random();
		randEmp = randString(num, "abcdefghijklmonpqrstuvwxyz", 5);
	}
	public void getRandClient()
	{
		Random num = new Random();
		randClient = randString(num, "abcdefghijklmonpqrstuvwxyz", 5);
	}
	public void testLogin() throws ClientProtocolException, IOException {
		String name = getAsString("post", "http://localhost:8080/GoGoGadget/LoginServlet?username=business&password=password");
		System.out.println(name);
	}
	public void testLogout() throws ClientProtocolException, IOException {
		String name = getAsString("get", "http://localhost:8080/GoGoGadget/LoginServlet");
		System.out.println(name);
	}
	
	public void testStart() throws ClientProtocolException, IOException {
		String name = getAsString("get", "http://localhost:8080/GoGoGadget/Business.jsp");
		System.out.println(name);
	}
	
	public void testCreateBusiness() throws ClientProtocolException, IOException {
		
		String name = getAsString("get", "http://localhost:8080/GoGoGadget/addBusinessServlet?busName=newBus&password=1234&email=bshaak@hotmail.ca&emailPassword=1234");
		assert(db.getProfile("newBus") != null);
		System.out.println(name);
		
	}
	
	public void testBusiness() throws ClientProtocolException, IOException{
		
		String name = getAsString("get", "http://localhost:8080/GoGoGadget/BusinessProfile.jsp?business=newBus&telephone=undefined&cell=undefined&city=undefined&email=bshaak@hotmail.ca&fax=undefined&postal=undefined&province=undefined");
		System.out.println(name);
	}
	
	public void testDeleteBusiness() throws ClientProtocolException, IOException {
		String name = getAsString("post", "http://localhost:8080/GoGoGadget/LoginServlet?username=newBus&password=1234");
		System.out.println(name);
		name = getAsString("post", "http://localhost:8080/GoGoGadget/RemoveBusinessServlet?");
		assert(db.getProfile("newBus") == null);
		System.out.println(name);
	}
	public void testCreateClient() throws ClientProtocolException, IOException {
		getRandClient();
		String name = getAsString("get", "http://localhost:8080/GoGoGadget/AddClient?first="+randClient+"&last=W");
		assert(!db.getClients(randClient, "W", null, null, null, null, null, null, null, null, "business").isEmpty());
		System.out.println(name);
		
	}
	public void testDeleteClient() throws ClientProtocolException, IOException {
		ArrayList<Client> al = db.getClients(randClient, "W", null, null, null, null, null, null, null, null, "business");
		Client c = al.get(0);
		if(c != null){
			Long clientID = c.getClientId();
			String name = getAsString("get", "http://localhost:8080/GoGoGadget/DeleteClientServlet?clientID="+ clientID.toString());
			System.out.println(name);
		}
		
	}
	
	public void testCreateEmployee() throws ClientProtocolException, IOException {
		//String name = getAsString("post", "http://localhost:8080/GoGoGadget/LoginServlet?username=business&password=password");
		//System.out.println(name);
		getRandEmpl();
		String name = getAsString("get", "http://localhost:8080/GoGoGadget/AddEmployeeServlet?first=" + randEmp);
		System.out.println(name);
	}
	
	public void testEditEmployee()throws ClientProtocolException, IOException {
		long i =0;
		Employee e = null;
		//String name = getAsString("post", "http://localhost:8080/GoGoGadget/LoginServlet?username=business&password=password");
		//System.out.println(name);
		while((i < 1000)){
		e = db.getEmployee(i, "business");
		if((e != null) && (e.getFirstName() == randEmp)){
			break;
		}
		}
		if(e != null){
			Long emp = e.getEmployeeID();
			String employeeID = emp.toString();
			String name = getAsString("post", "http://localhost:8080/GoGoGadget/UpdateEmployee2.jsp?employee=" + employeeID);
			System.out.println(name);
		}
		
	}
	
	public void testEmployee()throws ClientProtocolException, IOException{
	//	String name = getAsString("post", "http://localhost:8080/GoGoGadget/LoginServlet?username=business&password=password");
		
		long i =0;
		Employee e = null;
		while((i < 1000) && (e.getFirstName() != randEmp)){
		e = db.getEmployee(i, "business");
		}
		if(e != null){
			Long emp = e.getEmployeeID();
			String employeeID = emp.toString();
			String 	name = getAsString("get", "http://localhost:8080//GoGoGadget/EmployeeServlet?employee=" + employeeID);
			System.out.println(name);
		}
		
	}
	
	
	public void testDeleteEmployee() throws ClientProtocolException, IOException {
		
		//String name = getAsString("post", "http://localhost:8080/GoGoGadget/LoginServlet?username=business&password=password");
		long i =0;
		Employee e = null;
		while((i < 1000)){
		e = db.getEmployee(i, "business");
		i++;
		}
		if(e != null){
			if((e.getFirstName() == randEmp)){
				Long emp = e.getEmployeeID();
				String employeeID = emp.toString();
				String name = getAsString("get", "http://localhost:8080/GoGoGadget/RemoveEmployeeServlet?employee" + employeeID);
				System.out.println(name);
			}
		}
		
	}
	
	public void testAddJob() throws ClientProtocolException, IOException{
		//String name = getAsString("post", "http://localhost:8080/GoGoGadget/LoginServlet?username=business&password=password");
		ArrayList<Client> al = db.getClients(null, null, null, null, null, null, null, null, null, null, "business");
		Client c = al.get(0);
		if(c != null){
			Long clientID = c.getClientId();
			String name = getAsString("get", "http://localhost:8080/GoGoGadget/CreateJob.jsp?clientID=" + clientID + "&estPrice=8&actPrice=9&amtPaid=9");
			System.out.println(name);
		}
		
	}
	
	public void testJobInfo() throws ClientProtocolException, IOException{
		//String name = getAsString("post", "http://localhost:8080/GoGoGadget/LoginServlet?username=business&password=password");
		//System.out.println(name);
		Long i=(long) 0;
		Job j = null;
		while(i < 1000){
			j = db.getJob(i);
			i++;
			if(j!=null){
				String jobID = i.toString();
				String name = getAsString("get", "http://localhost:8080/GoGoGadget/JobInfo.jsp?jobID=" + jobID);
				System.out.println(name);
				break;
			}
		}
		
		
	}
	

	public void testDeleteJob()throws ClientProtocolException, IOException{
		//String name = getAsString("post", "http://localhost:8080/GoGoGadget/LoginServlet?username=business&password=password");
		//System.out.println(name);
		Long i=(long) 0;
		Job j = null;
		while(i < 1000){
			j = db.getJob(i);
			i++;
			if(j!=null){
				String jobID = i.toString();
				String name = getAsString("get", "http://localhost:8080/GoGoGadget/DeleteJobServlet?jobID=" + jobID);
				System.out.println(name);
				break;
			}
		}
		
	}
	
	public void testAddTimeSheet(long jobID, long empID)throws ClientProtocolException, IOException{
		
		//String name = getAsString("post", "http://localhost:8080/GoGoGadget/LoginServlet?username=business&password=password");
		//System.out.println(name);
		String name = getAsString("get", "http://localhost:8080/GoGoGadget/TimeSheet.jsp?jobID=" + jobID + "&empID=" + empID + "&date=2011-12-03");
		System.out.println(name);
		testLogout();
	}

	public void testMaps() throws ClientProtocolException, IOException {
		
		String name = getAsString("get", "http://localhost:8080/GoGoGadget/GoogleMapsPage.jsp?businessAddress=3555/West/King/Edward,/Vancouver&clientAddress=4378/cove/cliff/rd");
		System.out.println(name);
		// now get list of clients from db, get client Id and delete
		
	}
	public static void main(String [ ] args)
	{
		TestClient tc = new TestClient();
		
		try {
			tc.testStart();
			tc.testCreateBusiness();
			tc.testDeleteBusiness();
			tc.testLogin();
			tc.testCreateClient();
			tc.testDeleteClient();
			tc.testCreateEmployee();
			//tc.testEditEmployee();
			tc.testDeleteEmployee();
			tc.testAddJob();
		//	tc.testJobInfo();
			tc.testDeleteJob();
			tc.testMaps();
			tc.testLogout();
		} catch (ClientProtocolException e) {
			
			e.printStackTrace();
		} catch (IOException e) {
			
			e.printStackTrace();
		}
	}
}
